<template>
  <div id="userRegisterPage">
    <div class="register-container">
      <div class="register-decoration">
        <div class="decoration-content">
          <h2>加入我们</h2>
          <p>创建账号开始智能答题之旅</p>
          <div class="decoration-image">
            <img src="@/assets/education.svg" alt="Education illustration" />
          </div>
        </div>
      </div>

      <div class="register-card">
        <div class="register-header">
          <div class="logo-container">
            <img src="@/assets/logo.svg" alt="Logo" class="logo" />
          </div>
          <h2 class="register-title">用户注册</h2>
          <p class="register-subtitle">创建您的账号以开始使用</p>
        </div>

        <a-form class="register-form" :model="form" @submit="handleSubmit">
          <a-form-item field="userAccount">
            <a-input
              v-model="form.userAccount"
              placeholder="请输入账号"
              :style="{ width: '100%' }"
              size="large"
            >
              <template #prefix>
                <icon-user />
              </template>
            </a-input>
          </a-form-item>

          <a-form-item field="userPassword" tooltip="密码不小于 8 位">
            <a-input-password
              v-model="form.userPassword"
              placeholder="请输入密码"
              :style="{ width: '100%' }"
              size="large"
            >
              <template #prefix>
                <icon-lock />
              </template>
            </a-input-password>
          </a-form-item>

          <a-form-item field="checkPassword" tooltip="确认密码不小于 8 位">
            <a-input-password
              v-model="form.checkPassword"
              placeholder="请输入确认密码"
              :style="{ width: '100%' }"
              size="large"
            >
              <template #prefix>
                <icon-check-circle />
              </template>
            </a-input-password>
          </a-form-item>

          <a-form-item>
            <a-button type="primary" html-type="submit" long size="large">
              注册
            </a-button>
          </a-form-item>
        </a-form>

        <div class="register-footer">
          <p>
            已有账号?
            <a-link href="/user/login">立即登录</a-link>
          </p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive } from "vue";
import API from "@/api";
import { userRegisterUsingPost } from "@/api/userController";
import message from "@arco-design/web-vue/es/message";
import { useRouter } from "vue-router";
import { Message } from "@arco-design/web-vue";
import {
  IconUser,
  IconLock,
  IconCheckCircle,
} from "@arco-design/web-vue/es/icon";

const router = useRouter();

const form = reactive({
  userAccount: "",
  userPassword: "",
  checkPassword: "",
} as API.UserRegisterRequest);

/**
 * 提交
 */
const handleSubmit = async () => {
  try {
    // 判断两次输入的密码是否一致
    if (form.userPassword !== form.checkPassword) {
      Message.error("二次输入的密码不一致");
      return;
    }
    const res = await userRegisterUsingPost(form);
    // 注册成功，跳转到登录页面
    if (res.data.code === 0 && res.data.data) {
      message.success("注册成功");
      router.push({
        path: "/user/login",
        replace: true,
      });
    } else {
      message.error("注册失败，" + res.data.message);
    }
  } catch (e: any) {
    message.error("注册时发生错误：" + (e.message || "未知错误"));
  }
};
</script>

<style scoped>
#userRegisterPage {
  height: 100vh;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f5f7fe;
  padding: 20px;
  box-sizing: border-box;
}

.register-container {
  display: flex;
  width: 90%;
  max-width: 900px;
  height: auto;
  min-height: 520px;
  max-height: 90vh;
  box-shadow: 0 8px 40px rgba(0, 0, 0, 0.12);
  border-radius: 12px;
  overflow: hidden;
  flex-direction: row-reverse;
}

.register-card {
  flex: 1;
  background-color: #fff;
  padding: 32px;
  display: flex;
  flex-direction: column;
}

.register-header {
  text-align: center;
  margin-bottom: 24px;
}

.logo-container {
  margin-bottom: 20px;
}

.logo {
  height: 50px;
}

.register-title {
  font-size: 24px;
  font-weight: 600;
  color: var(--color-text-1);
  margin: 0 0 8px;
}

.register-subtitle {
  font-size: 14px;
  color: var(--color-text-3);
  margin: 0;
}

.register-form {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.register-footer {
  text-align: center;
  margin-top: 20px;
  color: var(--color-text-3);
}

.register-decoration {
  flex: 1;
  background: linear-gradient(135deg, #7cb8ff, #165dff);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 32px;
  color: #fff;
}

.decoration-content {
  text-align: center;
}

.decoration-content h2 {
  font-size: 26px;
  font-weight: 600;
  margin-bottom: 12px;
}

.decoration-content p {
  font-size: 15px;
  margin-bottom: 24px;
  opacity: 0.9;
}

.decoration-image img {
  width: 200px;
  height: 200px;
  filter: drop-shadow(0 4px 12px rgba(0, 0, 0, 0.15));
}

/* 响应式调整 */
@media (max-width: 768px) {
  .register-container {
    flex-direction: column-reverse;
    height: auto;
    width: 95%;
  }

  .register-decoration {
    display: none; /* 在小屏幕上隐藏装饰部分 */
  }

  .register-card {
    padding: 24px;
  }
}
</style>
